package com.mu.manger;

import com.alibaba.fastjson.JSON;
import com.mu.domain.MessageEntity;
import com.mu.domain.UserMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author shang
 */
@Slf4j
public class MessageCallBack {


    private static volatile MessageCallBack messageCallBack;
    private static ConcurrentHashMap<String, String> backMsgMap = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, Map<String, UserMessage>> userMessageMap = new ConcurrentHashMap<>();

    /**
     * 用次数来控制，这样最大时间可以拉到14-15s;  还有用相同的问题来控制
     */

    public static MessageCallBack getSingleton(){
        if (messageCallBack == null) {
            synchronized (MessageEntity.class) {
                if (messageCallBack == null) {
                    messageCallBack = new MessageCallBack();
                    return messageCallBack;
                }
            }
        }
        return messageCallBack;
    }
    public void setBackMsg(String userId, String backMsg){
        log.info("获取片段回复 userId：{}, backMsg:{}", userId, backMsg);
        if (backMsgMap.containsKey(userId)) {
            backMsgMap.put(userId, backMsgMap.get(userId) + backMsg);
        }else {
            backMsgMap.put(userId, backMsg);
        }
    }

    public void setQuestion(String userId, String question){
        if (userMessageMap.containsKey(userId)) {
            userMessageMap.get(userId)
                    .put(question,UserMessage.builder().userId(userId).question(question).number(1).build());
        }else {
            Map<String, UserMessage> questionMap = new HashMap<>(8);
            questionMap.put(question,UserMessage.builder().userId(userId).question(question).number(1).build());
            userMessageMap.put(userId, questionMap);
        }
        log.info("存储用户问题 userId: {}, question:{}", userId, question);
    }

    public void sendBack(String param){
        String[] split = param.split("&");
        String userId = split[0];
        String question = split[1];
        String answer = backMsgMap.get(param);
        Map<String, UserMessage> questionMessageMap = userMessageMap.get(userId);
        UserMessage userMessage = questionMessageMap.get(question);
        userMessage.setAnswer(answer);
        log.info("获取回复 userId:{}, question:{}, answer: {}", userId, userMessage.getQuestion(), answer);
    }

    /**
     * 判断是否是重复问题，前一个请求还在处理中
     * @param userId 用户
     * @param userQuestion 问题
     * @return 结果  true的话是在处理中，无需重复发送AI请求
     */
    public boolean isRepeatQuestion(String userId, String userQuestion){
        return (userMessageMap.containsKey(userId) && userMessageMap.get(userId).containsKey(userQuestion));
    }

    public void userQuestionNumberAdd(String userId, String userQuestion){
        UserMessage userMessage = userMessageMap.get(userId).get(userQuestion);
        userMessage.setNumber(userMessage.getNumber() + 1);
    }

    /**
     * 是否已经问过该问题，但是没有返回
     * 如果取出答案数据，则可以将数据清除
     * @param userId userId
     * @return 回答
     */
    public UserMessage getBackUserMessage(String userId, String userQuestion){
        if (userMessageMap.containsKey(userId) && userMessageMap.get(userId).containsKey(userQuestion)) {
            return userMessageMap.get(userId).get(userQuestion);
        }
        return null;
    }

    /**
     * 删除回复
     * @param userId 用户id
     * @param question 回答
     */
    public void remove(String userId, String question){
        userMessageMap.get(userId).remove(question);
    }

}
